package com.example.countscore;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class MyTask implements Runnable{

    private static final String TAG = "MyTask";

    private Handler handler;

    public void setHandler(Handler handler) {
        this.handler = handler;
    }

    public void run(){
        Log.i(TAG, "run: task....");
//        try {
//            Thread.sleep(3000);//延迟
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }

//        //获取网络数据
//        URL url =null;
//        try{
//            url = new URL("http://www.usd-cny.com/icbc.htm");
//            HttpURLConnection http = (HttpURLConnection)url.openConnection();
//            InputStream in = http.getInputStream();
//
//            String html = inputStream2String(in);
//            Log.i(TAG, "run: html=" + html);
//
//        }catch (MalformedURLException e){
//            e.printStackTrace();
//        }catch (IOException e){
//            e.printStackTrace();
//        }


        List<String> list_rate = new ArrayList<String>();
        //使用Jsoup
        try {
            Document doc = Jsoup.connect("https://usd-cny.com/").get();
            Element firstTable = doc.getElementsByTag("table").first();
            Elements trs = firstTable.getElementsByTag("tr");
            trs.remove(0); //删除表头

            //初始化Bundle，存储计算所得汇率
            Bundle bdl = new Bundle();
            for(Element tr:trs){
                Elements tds = tr.getElementsByTag("td");
                Element td1 = tds.get(0);
                Element td2 = tds.get(4);
                Log.i(TAG, "run: td2="+td2.text());

                //列表数据
                list_rate.add(td1.text()+"----->"+td2.text());
                Log.i(TAG, "runList: "+td1.text()+"----->"+td2.text());

                if("美元".equals(td1.text())){
                    float rate = 100f / Float.parseFloat(td2.text());
                    Log.i(TAG, "run: td1="+td1.text()+"----->rate="+rate);
                    bdl.putFloat("key_dollar",rate);
                }

                if("欧元".equals(td1.text())){
                    float rate = 100f / Float.parseFloat(td2.text());
                    Log.i(TAG, "run: td1="+td1.text()+"----->rate="+rate);
                    bdl.putFloat("key_euro",rate);
                }

                if("韩币".equals(td1.text())){
                    float rate = 100f / Float.parseFloat(td2.text());
                    Log.i(TAG, "run: td1="+td1.text()+"----->rate="+rate);
                    bdl.putFloat("key_won",rate);
                }

                //发送消息
                Message msg = handler.obtainMessage(6);
                msg.obj = bdl;
                handler.sendMessage(msg);
                Log.i(TAG, "run: 消息已发送");


            }
            //发送消息
            Message msg2 = handler.obtainMessage(7, list_rate);
            handler.sendMessage(msg2);
            Log.i(TAG, "run: 消息已发送");


        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
